# -*- encoding: utf-8 -*-
##############################################################################
#    
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 Tiny SPRL (&lt;http://tiny.be&gt;).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.     
#
##############################################################################

import logging 
from osv import osv, fields
import netsvc
import pooler
from tools.translate import _
import decimal_precision as dp
from osv.orm import browse_record, browse_null
import time
from datetime import datetime, date

_logger = logging.getLogger(__name__)

# Products extension
class actual_product_cost(osv.osv):
    _name    = "product.template"
    _inherit = "product.template"
 
    _columns = {
        'price_without_tax' : fields.float('Price Without Tax', digits_compute=dp.get_precision('Product Price'), help="Current product cost price without tax for maintain labor of the tax management"),
        }

actual_product_cost()


class stock_picking(osv.osv):
    _name= "stock.picking"
    _inherit = "stock.picking"
    
    def do_partial(self, cr, uid, ids, partial_datas, context=None):
        """ Inherited method to add actual cost computation 
            when standard cost is required by an article 
        @param partial_datas : Dictionary containing details of partial picking
                          like partner_id, partner_id, delivery_date,
                          delivery moves with product_id, product_qty, uom
        @return: Dictionary of values 
        """
        res = super(stock_picking, self).do_partial(cr, uid, ids, partial_datas, context)
        product_obj = self.pool.get('product.product')
        tax_obj = self.pool.get('account.tax')
        uom_obj = self.pool.get('product.uom')
        currency_obj = self.pool.get('res.currency')
        for pick in self.browse(cr, uid, ids, context=context):
            for move in pick.move_lines:
                partial_data = partial_datas.get('move%s'%(move.id), {})
                product_price = partial_data.get('product_price', 0.0)
                product_uom = partial_data.get('product_uom',False)
                product_qty = partial_data.get('product_qty',0.0)
                product_currency = partial_data.get('product_currency',False)
                #if move.state in ('done', 'cancel'):
                #    continue
                if (pick.type == 'in') and (move.product_id.cost_method == 'standard'):
                    move_currency_id = move.company_id.currency_id.id
                    product = product_obj.browse(cr, uid, move.product_id.id)
                    qty = uom_qty = uom_obj._compute_qty(cr, uid, product_uom, product_qty, product.uom_id.id)
                    if (qty > 0):
#                        new_price = currency_obj.compute(cr, uid, product_currency,
#                                move_currency_id, product_price)
#                        new_price = uom_obj._compute_price(cr, uid, product_uom, new_price,
#                                product.uom_id.id)
                        # Validate the market cost to be used #in (average or actual, depends which one is grater than)
                        # Get the standard price
#                        tax = tax_obj.browse(cr, uid, move.product_id.supplier_taxes_id)
                        taxes = tax_obj.compute_all(cr, uid, move.product_id.supplier_taxes_id, move.price_unit, 1)
                        actual_price = taxes['total_included']
                        _logger.warning('Actual cost price with tax is: %f' % actual_price)
#                        amount = round(tax.get('amount', 0.0))
#                        actual_price = move.price_unit * (1 + amount)
                        if (product.qty_available > 0) and (product.standard_price > actual_price):
                            new_std_price = product.standard_price
                            new_price = product.price_without_tax
                        else:
                            new_std_price = actual_price
                            new_price = move.price_unit

                        product_obj.write(cr, uid, [product.id], {'price_without_tax': new_price, 'standard_price': new_std_price})

stock_picking()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
